<template>
	<div class="cms-channel-container">
		<el-card shadow="hover" class="mb10" v-auth="'cms:channel:list'">
			<el-input size="small" v-model="search.name" placeholder="请输入栏目名称" prefix-icon="el-icon-search" style="max-width: 180px;" class="ml10"></el-input>
			<el-button size="small" icon="el-icon-search" type="primary" class="ml10" @click="onTableSearch()">查询</el-button>
			<el-button size="small" icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
		</el-card>
		<el-card shadow="hover">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-button v-auth="'cms:channel:add'" size="small" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddChannel()">新增</el-button>
					<el-table ref="tableChannelRef" stripe style="width: 100%" row-key="id" 
						:data="tableData.data"
						:expand-row-keys="[tableData.data[0]?.id]" 
						:tree-props="{ children: 'children'}"
						@row-click="onTableRowClick"
						>
						<el-table-column label="栏目名称" prop="name" show-overflow-tooltip></el-table-column>
						<el-table-column label="状态" show-overflow-tooltip width="70">
							<template #default="scope">
								<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
								<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
								<span v-else class="color-danger">删除</span>
							</template>
						</el-table-column>
						<el-table-column label="排序" prop="orderNum" width="80" show-overflow-tooltip></el-table-column>
						<el-table-column label="操作" show-overflow-tooltip width="80">
							<template #default="scope">
								<el-dropdown>
									<span class="el-dropdown-link">
										<el-button size="mini" type="text">操作 <i class="fa fa-angle-down"></i></el-button>
									</span>
									<template #dropdown>
										<center>
											<el-dropdown-menu>
												<el-dropdown-item>
													<el-button v-auth="'cms:channel:add'" v-if="scope.row.parentId != 0" size="mini" type="text" @click="onOpenAddChannel(scope.row)">新增</el-button>
												</el-dropdown-item>
												<el-dropdown-item>
													<el-button v-auth="'cms:channel:edit'" v-if="scope.row.parentId != 0" size="mini" type="text" @click="onOpenEditChannel(scope.row)">修改</el-button>
												</el-dropdown-item>
												<el-dropdown-item>
													<el-button v-auth="'cms:channel:remove'" v-if="scope.row.parentId != 0" size="mini" type="text" @click="onTabelRowDel(scope.row)">删除</el-button>
												</el-dropdown-item>
												<el-dropdown-item>
													<el-button size="mini" type="text" @click="onOpenCopyChannel(scope.row)">复制编码</el-button>
												</el-dropdown-item>
											</el-dropdown-menu>
										</center>
									</template>
								</el-dropdown>
							</template>
						</el-table-column>
					</el-table>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<ConfigSite :siteId="search.siteId" v-show="card == 'config-site'" />
					<ConfigChannel :model="channel" v-show="card == 'config-channel'"/>
				</el-col>
			</el-row>
		</el-card>
		<AddChannel ref="addChannelRef" @submit="onTableSearch"/>
		<EditChannel ref="editChannelRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddChannel from '/@/views/cms/channel/addChannel.vue';
import EditChannel from '/@/views/cms/channel/editChannel.vue';
import ConfigSite from '/@/views/cms/channel/configSite.vue';
import ConfigChannel from '/@/views/cms/channel/configChannel.vue';
import { getChannelList, delChannel } from '/@/api/cms/channel/index';
import { lineToTreeData, copyText } from '/@/utils/commonFunction';
export default defineComponent({
	name: 'cms/channel',
	props: {
		// 绑定值
		model: {
			type: Object,
			default: () => {},
		},
	},
	components: { AddChannel, EditChannel, ConfigSite, ConfigChannel },
	setup(props, context) {
		const tableChannelRef = ref();
		const addChannelRef = ref();
		const editChannelRef = ref();
		const state = reactive({
			card : 'config-site',
			channel: {},
			search:{
				siteId: '',
				name: ''
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getChannelList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = lineToTreeData(res.data);
					state.tableData.total = res.total;
				}
			});
		}

		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
			context.emit('submit');
		}
		//重置
		const onResetSearch = () => {
			state.search.name = undefined;
			loadTableData();
		}
		// 复制编码
		const onOpenCopyChannel = (row: object) => {
			copyText(row.id);
		}
		// 打开新增栏目弹窗
		const onOpenAddChannel = (row: object) => {
			addChannelRef.value.openDialog(row || props.model);
		};
		// 打开编辑栏目弹窗
		const onOpenEditChannel = (row: object) => {
			editChannelRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.name + '】栏目信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delChannel([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						onTableSearch();
					}
				});
			}).catch(() => {});
		};
		// 当行点击时
		const onTableRowClick = (row: object) => {
			if(row.parentId == 0){
				state.card = 'config-site';
			}else{
				state.card = 'config-channel';
				state.channel = row;
			}
		}
		// 页面加载时
		onMounted(() => {
			state.search.siteId = props.model.siteId;
			loadTableData();
		});
		return {
			tableChannelRef,
			addChannelRef,
			editChannelRef,
			onTableSearch,
			onResetSearch,
			onOpenCopyChannel,
			onOpenAddChannel,
			onOpenEditChannel,
			onTabelRowDel,
			onTableRowClick,
			...toRefs(state),
		};
	},
});
</script>